package com.android.pinprovince.adapt;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.pinprovince.R;
import com.android.pinprovince.model.group.MainList;
import com.android.pinprovince.pojo.WidthHeighGlidePojo;
import com.android.pinprovince.utils.CommonUtils;
import com.android.pinprovince.utils.DisplayUtil;
import com.android.pinprovince.utils.ObjectUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/8/10.
 */
public class GridFragment1TopAdapt extends BaseAdapter {
    private LayoutInflater Inflater;
    private Context content;
    private List<MainList.MainListdata.Mainlistdata> listdata;
    String type ="0";
    //private static String empty="                 ";
//    private static String empty1="             ";

    private static String empty="";
    private static String empty1="";
    private int witdh;
    public GridFragment1TopAdapt(Context content, List<MainList.MainListdata.Mainlistdata> listdata) {
        this.listdata = listdata;
        this.content = content;
        this.Inflater = LayoutInflater.from(content);

        witdh =   (DisplayUtil.getWindowWidth((Activity)content));

    }
    @Override
    public int getCount() {
        return listdata.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        ViewHolder holder = null;
        holder = new ViewHolder();
        if (convertView == null) {

            convertView = Inflater.inflate(R.layout.item_list_main, null);

            holder.picLinearlayout= (View) convertView.findViewById(R.id.picLinearlayout);
            holder.item_list_main_ing_ll = (LinearLayout) convertView.findViewById(R.id.item_list_main_ing_ll);

            holder.main_list_item_image = (ImageView) convertView.findViewById(R.id.main_list_item_image);
            holder.title_type = (TextView) convertView.findViewById(R.id.title_type);
            holder.main_list_item_count = (TextView) convertView.findViewById(R.id.main_list_item_count);
            holder.main_list_item_tuannumber = (TextView) convertView.findViewById(R.id.main_list_item_tuannumber);
            holder.ll_start_soon_num = (TextView) convertView.findViewById(R.id.ll_start_soon_num);
            holder.ll_free_num = (TextView) convertView.findViewById(R.id.ll_free_num);
            holder.main_list_item_alltuannumber2 = (TextView) convertView.findViewById(R.id.main_list_item_alltuannumber2);
            holder.main_list_item_money_after = (TextView) convertView.findViewById(R.id.main_list_item_money_after);
            holder.main_list_item_title = (TextView) convertView.findViewById(R.id.main_list_item_title);
            holder.item_list_sort_lottory_time = (TextView) convertView.findViewById(R.id.item_list_sort_lottory_time);
            holder.main_list_item_stock = (TextView) convertView.findViewById(R.id.main_list_item_stock);
            holder.ll_result = (LinearLayout) convertView.findViewById(R.id.ll_result);
            holder.ll_free = (LinearLayout) convertView.findViewById(R.id.ll_free);
            holder.ll_start_soon = (LinearLayout) convertView.findViewById(R.id.ll_start_soon);
            holder.main_list_item_go_kai = (LinearLayout) convertView.findViewById(R.id.main_list_item_go_kai);
            holder.ll_price_aa = (LinearLayout) convertView.findViewById(R.id.ll_price_aa);
            holder.ll_price_normal = (LinearLayout) convertView.findViewById(R.id.ll_price_normal);
            holder.item_list_sort_aa_total = (TextView) convertView.findViewById(R.id.item_list_sort_aa_total);
            holder.item_list_sort_aa_before = (TextView) convertView.findViewById(R.id.item_list_sort_aa_before);
            holder.item_list_sort_aa_tuanshu2 = (TextView) convertView.findViewById(R.id.item_list_sort_aa_tuanshu2);
            holder.item_list_sort_aa_after = (TextView) convertView.findViewById(R.id.item_list_sort_aa_after);


            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();

        }

        MainList.MainListdata.Mainlistdata mainlistdata = listdata.get(position);
        holder.item_list_main_ing_ll.setVisibility(View.GONE);
        holder.title_type.setVisibility(View.GONE);

        ((TextView) holder.item_list_sort_aa_before).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        WidthHeighGlidePojo pojo = new WidthHeighGlidePojo(2, witdh);
        CommonUtils.SetViewWidthH(pojo,holder.picLinearlayout);
        ObjectUtils.photo2(content, mainlistdata.getThumb(), ((ImageView) holder.main_list_item_image));


        holder.main_list_item_count.setText(mainlistdata.getDiscount_price() + "折");
        holder.main_list_item_tuannumber.setText(mainlistdata.getTeam_num() + "人团");
        holder.ll_start_soon_num.setText(mainlistdata.getTeam_num() + "人团");
        holder.ll_free_num.setText(mainlistdata.getTeam_num() + "人团");
        holder.main_list_item_alltuannumber2.setText("已团" + mainlistdata.getTeam_people_num() + "件");
        String price1 = mainlistdata.getTeam_price();
        SpannableString msp = new SpannableString("￥" + price1);
        msp.setSpan(new AbsoluteSizeSpan(13, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.main_list_item_money_after.setText(msp);
        int endtime = Integer.parseInt(mainlistdata.getEnd_time());
        final int starttime = Integer.parseInt(mainlistdata.getStart_time());
        final int run_time = mainlistdata.getRun_time();

        switch (mainlistdata.getTypeid()) {
            case "1":
                holder.title_type.setText( "拼团");
                holder.main_list_item_title.setText(empty1 + mainlistdata.getName());
                break;
            case "2":
                holder.title_type.setText("新人专享");
                holder.main_list_item_title.setText( empty + mainlistdata.getName());
                break;
            case "3":
                holder.title_type.setText( "限时秒杀");
                holder.main_list_item_title.setText( empty + mainlistdata.getName());
                break;
            case "4":
                holder.title_type.setText( "限时秒杀");
                holder.main_list_item_title.setText( empty+ mainlistdata.getName());
                break;
            case "5":
                holder.title_type.setText("免费试用");
                holder.main_list_item_title.setText(empty + mainlistdata.getName());
                break;
            case "6":
                holder.title_type.setText( "抽奖");
                holder.main_list_item_title.setText( empty1 + mainlistdata.getName());
                break;
            case "7":
                holder.title_type.setText( "AA团");
                holder.main_list_item_title.setText( empty1+ mainlistdata.getName());

                break;
            case "8":
                holder.title_type.setText( "推广团");
                holder.main_list_item_title.setText( empty + mainlistdata.getName());

                break;
            case "9":
                holder.title_type.setText( "自选团");
                holder.main_list_item_title.setText( empty+ mainlistdata.getName());
                break;
            case "10":
                holder.title_type.setText( "阶梯团");
                holder.main_list_item_title.setText( empty + mainlistdata.getName());
                break;
        }

        String strendTime;
        switch (type) {
            case "3":
                holder.main_list_item_count.setText( "试用");
                holder.item_list_sort_lottory_time.setVisibility(View.VISIBLE);
                strendTime = ObjectUtils.getStrTime(mainlistdata.getEnd_time(), "MM月dd日 HH:mm");
                holder.item_list_sort_lottory_time.setText( "截止时间" + strendTime);

                holder.main_list_item_stock.setVisibility(View.VISIBLE);
                if (run_time > endtime && run_time > starttime) {
                    holder.main_list_item_stock.setText("活动已经结束");

                    holder.ll_result.setVisibility(View.VISIBLE);
                    holder.ll_free.setVisibility(View.GONE);
                    holder.ll_start_soon.setVisibility(View.GONE);
                } else if (run_time > starttime && run_time < endtime) {
                    holder.main_list_item_stock.setText("限量" + mainlistdata.getLuck_num() + "件");
                    holder.ll_result.setVisibility(View.GONE);
                    holder.ll_free.setVisibility(View.VISIBLE);
                    holder.ll_start_soon.setVisibility(View.GONE);
                } else if (run_time > starttime && run_time < endtime) {
                } else if (run_time < starttime) {
                    holder.main_list_item_stock.setText( "限量" + mainlistdata.getLuck_num() + "件");
                    holder.ll_result.setVisibility(View.GONE);
                    holder.ll_free.setVisibility(View.GONE);
                    holder.ll_start_soon.setVisibility(View.VISIBLE);
                }
                holder.main_list_item_alltuannumber2.setText("￥" + mainlistdata.getPrice());
                ((TextView) holder.main_list_item_alltuannumber2).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                break;
            case "6":
                holder.main_list_item_count.setText("抽奖");
                holder.item_list_sort_lottory_time.setVisibility(View.VISIBLE);
                strendTime = ObjectUtils.getStrTime(mainlistdata.getEnd_time(), "MM月dd日 HH:mm");
                holder.item_list_sort_lottory_time.setText("截止时间" + strendTime);
                if (run_time > endtime && run_time > starttime) {

                    holder.ll_result.setVisibility(View.VISIBLE);
                    holder.main_list_item_go_kai.setVisibility(View.GONE);
                    holder.ll_start_soon.setVisibility(View.GONE);
                } else if (run_time > starttime && run_time < endtime) {
//                    holder.setVisible(R.id.ll_result, false);
//                    holder.setVisible(R.id.main_list_item_go_kai, true);
//                    holder.setVisible(R.id.ll_start_soon, false);
                    holder.ll_result.setVisibility(View.GONE);
                    holder.main_list_item_go_kai.setVisibility(View.VISIBLE);
                    holder.ll_start_soon.setVisibility(View.GONE);

                } else if (run_time < starttime) {
//                    holder.setVisible(R.id.ll_result, false);
//                    holder.setVisible(R.id.main_list_item_go_kai, false);
//                    holder.setVisible(R.id.ll_start_soon, true);

                    holder.ll_result.setVisibility(View.GONE);
                    holder.main_list_item_go_kai.setVisibility(View.GONE);
                    holder.ll_start_soon.setVisibility(View.VISIBLE);


                }
                holder.main_list_item_alltuannumber2.setText( "￥" + mainlistdata.getPrice());
                ((TextView) holder.main_list_item_alltuannumber2).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                break;
            case "7":
//                holder.setVisible(R.id.ll_price_aa, true);
//                holder.setVisible(R.id.main_list_item_go_kai, true);
//                holder.setVisible(R.id.ll_price_normal, false);

                holder.ll_price_aa.setVisibility(View.VISIBLE);
                holder.main_list_item_go_kai.setVisibility(View.VISIBLE);
                holder.ll_price_normal.setVisibility(View.GONE);

                SpannableString msp_aa = new SpannableString("￥" + mainlistdata.getTeam_price());
                msp_aa.setSpan(new AbsoluteSizeSpan(13, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.item_list_sort_aa_total.setText( msp_aa);
                holder.item_list_sort_aa_before.setText( "￥" + mainlistdata.getPrice());
                holder.item_list_sort_aa_tuanshu2.setText( mainlistdata.getTeam_num() + "人团总价:");
                double v = Double.parseDouble(price1);
                int tuan = Integer.parseInt(mainlistdata.getTeam_num());
                String price = ObjectUtils.div(v, tuan, 2) + "";
                String[] split = price.split("\\.");
                if (split[1].equals("0")) {
                    price = split[0];
                }
                holder.item_list_sort_aa_after.setText( "每人" + price + "元");
                break;
            default:
                holder.main_list_item_go_kai.setVisibility(View.VISIBLE);

                break;

        }


        return convertView;
    }



/*

    // 拼省省要隐藏的
                holder.getView(R.id.item_list_main_ing_ll).setVisibility(View.GONE);
                holder.getView(title_type).setVisibility(View.GONE);

                ((TextView) holder.getView(R.id.item_list_sort_aa_before)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                ObjectUtils.photo2(content, listdata.getThumb(), ((ImageView) holder.getView(R.id.main_list_item_image)));
                holder.setText(R.id.main_list_item_count, listdata.getDiscount_price() + "折");
                holder.setText(R.id.main_list_item_tuannumber, listdata.getTeam_num() + "人团");
                holder.setText(R.id.ll_start_soon_num, listdata.getTeam_num() + "人团");
                holder.setText(R.id.ll_free_num, listdata.getTeam_num() + "人团");
                holder.setText(R.id.main_list_item_alltuannumber2, "已团" + listdata.getTeam_people_num() + "件");
    String price1 = listdata.getTeam_price();
    SpannableString msp = new SpannableString("￥" + price1);
                msp.setSpan(new AbsoluteSizeSpan(13, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.setText(R.id.main_list_item_money_after, msp);
    int endtime = Integer.parseInt(listdata.getEnd_time());
    final int starttime = Integer.parseInt(listdata.getStart_time());
    final int run_time = listdata.getRun_time();

                switch (listdata.getTypeid()) {
        case "1":
            holder.setText(title_type, "拼团");
            holder.setText(R.id.main_list_item_title, empty1 + listdata.getName());
            break;
        case "2":
            holder.setText(title_type, "新人专享");
            holder.setText(R.id.main_list_item_title, empty + listdata.getName());
            break;
        case "3":
            holder.setText(title_type, "限时秒杀");
            holder.setText(R.id.main_list_item_title, empty + listdata.getName());
            break;
        case "4":
            holder.setText(title_type, "限时秒杀");
            holder.setText(R.id.main_list_item_title, empty+ listdata.getName());
            break;
        case "5":
            holder.setText(title_type, "免费试用");
            holder.setText(R.id.main_list_item_title, empty + listdata.getName());
            break;
        case "6":
            holder.setText(title_type, "抽奖");
            holder.setText(R.id.main_list_item_title, empty1 + listdata.getName());
            break;
        case "7":
            holder.setText(title_type, "AA团");
            holder.setText(R.id.main_list_item_title, empty1+ listdata.getName());

            break;
        case "8":
            holder.setText(title_type, "推广团");
            holder.setText(R.id.main_list_item_title, empty + listdata.getName());

            break;
        case "9":
            holder.setText(title_type, "自选团");
            holder.setText(R.id.main_list_item_title, empty+ listdata.getName());
            break;
        case "10":
            holder.setText(title_type, "阶梯团");
            holder.setText(R.id.main_list_item_title, empty + listdata.getName());
            break;
    }

    String strendTime;
                switch (type) {
        case "3":
            holder.setText(R.id.main_list_item_count, "试用");
            holder.setVisible(R.id.item_list_sort_lottory_time, true);
            strendTime = ObjectUtils.getStrTime(listdata.getEnd_time(), "MM月dd日 HH:mm");
            holder.setText(R.id.item_list_sort_lottory_time, "截止时间" + strendTime);
            holder.setVisible(R.id.main_list_item_stock, true);
            if (run_time > endtime && run_time > starttime) {
                holder.setText(R.id.main_list_item_stock, "活动已经结束");
                holder.setVisible(R.id.ll_result, true);
                holder.setVisible(R.id.ll_free, false);
                holder.setVisible(R.id.ll_start_soon, false);
            } else if (run_time > starttime && run_time < endtime) {
                holder.setText(R.id.main_list_item_stock, "限量" + listdata.getLuck_num() + "件");
                holder.setVisible(R.id.ll_result, false);
                holder.setVisible(R.id.ll_free, true);
                holder.setVisible(R.id.ll_start_soon, false);
            } else if (run_time < starttime) {
                holder.setText(R.id.main_list_item_stock, "限量" + listdata.getLuck_num() + "件");
                holder.setVisible(R.id.ll_result, false);
                holder.setVisible(R.id.ll_free, false);
                holder.setVisible(R.id.ll_start_soon, true);
            }
            holder.setText(R.id.main_list_item_alltuannumber2, "￥" + listdata.getPrice());
            ((TextView) holder.getView(R.id.main_list_item_alltuannumber2)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            break;
        case "6":
            holder.setText(R.id.main_list_item_count, "抽奖");
            holder.setVisible(R.id.item_list_sort_lottory_time, true);
            strendTime = ObjectUtils.getStrTime(listdata.getEnd_time(), "MM月dd日 HH:mm");
            holder.setText(R.id.item_list_sort_lottory_time, "截止时间" + strendTime);
            if (run_time > endtime && run_time > starttime) {
                holder.setVisible(R.id.ll_result, true);
                holder.setVisible(R.id.main_list_item_go_kai, false);
                holder.setVisible(R.id.ll_start_soon, false);
            } else if (run_time > starttime && run_time < endtime) {
                holder.setVisible(R.id.ll_result, false);
                holder.setVisible(R.id.main_list_item_go_kai, true);
                holder.setVisible(R.id.ll_start_soon, false);
            } else if (run_time < starttime) {
                holder.setVisible(R.id.ll_result, false);
                holder.setVisible(R.id.main_list_item_go_kai, false);
                holder.setVisible(R.id.ll_start_soon, true);
            }
            holder.setText(R.id.main_list_item_alltuannumber2, "￥" + listdata.getPrice());
            ((TextView) holder.getView(R.id.main_list_item_alltuannumber2)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            break;
        case "7":
            holder.setVisible(R.id.ll_price_aa, true);
            holder.setVisible(R.id.main_list_item_go_kai, true);
            holder.setVisible(R.id.ll_price_normal, false);
            SpannableString msp_aa = new SpannableString("￥" + listdata.getTeam_price());
            msp_aa.setSpan(new AbsoluteSizeSpan(13, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.setText(R.id.item_list_sort_aa_total, msp_aa);
            holder.setText(R.id.item_list_sort_aa_before, "￥" + listdata.getPrice());
            holder.setText(R.id.item_list_sort_aa_tuanshu2, listdata.getTeam_num() + "人团总价:");
            double v = Double.parseDouble(price1);
            int tuan = Integer.parseInt(listdata.getTeam_num());
            String price = ObjectUtils.div(v, tuan, 2) + "";
            String[] split = price.split("\\.");
            if (split[1].equals("0")) {
                price = split[0];
            }
            holder.setText(R.id.item_list_sort_aa_after, "每人" + price + "元");
            break;
        default:
            holder.setVisible(R.id.main_list_item_go_kai, true);

            break;

    }*/




    static class ViewHolder {

//        ImageView item_grid_detail_like_img;
//        TextView item_grid_detail_like_title;
//        TextView item_grid_detail_like_number_after;
//        TextView item_grid_detail_like_number_before;
//        TextView item_grid_detail_like_tuanshu;
View picLinearlayout;
        ImageView main_list_item_image;
        LinearLayout item_list_main_ing_ll;
        TextView title_type;
        TextView main_list_item_count;
        TextView main_list_item_tuannumber;
        TextView ll_start_soon_num;
        TextView ll_free_num;
        TextView main_list_item_alltuannumber2;
        TextView main_list_item_money_after;
        TextView main_list_item_title;
        TextView item_list_sort_lottory_time;
        TextView main_list_item_stock;
        LinearLayout ll_result;
        LinearLayout ll_free;
        LinearLayout ll_start_soon;
        LinearLayout main_list_item_go_kai;
        LinearLayout ll_price_aa;
        LinearLayout ll_price_normal;
        TextView item_list_sort_aa_total;
        TextView item_list_sort_aa_before;
        TextView item_list_sort_aa_tuanshu2;
        TextView item_list_sort_aa_after;
                ;

    }
}
